package com.thunbu.thunbu_meeting_system.utils

import com.thunbu.lib_common.config.CommConstants.TOKEN
import com.thunbu.lib_common.utils.ActivityManager
import com.thunbu.lib_common.utils.MFSPHelper
import com.thunbu.thunbu_meeting_system.activity.LoginActivity
import com.thunbu.thunbu_meeting_system.bean.*
import com.thunbu.thunbu_meeting_system.gotoActivity

object AppUtils {
    const val BIND_RESULT="bindResult"
    const val AUTH_RESULT="authResult"
    const val USER_INFO="userInfo"
    const val ASSET_TYPE="asset_type"
    const val ADDRESS_INFO="address_info"
    const val DEPARTMENT_INFO="department_info"
    const val DEPARTMENT_USER_INFO="department_user_info"

//    var homePageTypeVOList: List<HomePageTypeVO>? = null //资产类型列表
    var mAssetType:AssetTypeModel? = null //资产类型列表
    var mAddressInfo:List<AddressModel>? = null //存放地址列表
    var mDepartmentInfo:DepartmentModel? = null //部门列表
    var mDepartmentUserInfo:DepartmentModel? = null //部门人员列表

    fun setBindResult(isSuccess:Boolean?){
        MFSPHelper.setBoolean(BIND_RESULT, isSuccess)
    }

    fun isBinded():Boolean{
        return MFSPHelper.getBoolean(BIND_RESULT,false)
    }

    fun setAuthResult(isSuccess: Boolean?){
        MFSPHelper.setBoolean(AUTH_RESULT, isSuccess)
    }

    fun isAuthed():Boolean{
        return MFSPHelper.getBoolean(AUTH_RESULT,false)
    }

    fun setToken(token:String?){
        MFSPHelper.setString(TOKEN, token)
    }

    fun getToken():String?{
        return MFSPHelper.getString(TOKEN,null)
    }

    fun setUserInfo(userInfo: UserInfo?){
        userInfo?.run {
            MFSPHelper.saveObject(USER_INFO,userInfo)
        }
    }

    fun getUserInfo():UserInfo?{
        if(MFSPHelper.hasKey(USER_INFO))
        return MFSPHelper.readObject(USER_INFO,UserInfo::class.java)
        else return null
    }

    fun setAssetType(assetType:AssetTypeModel?){
        assetType?.run {
            mAssetType = assetType
            MFSPHelper.saveObject(ASSET_TYPE,assetType)
        }
    }

    fun getAssetType():AssetTypeModel?{
        if(mAssetType != null)
            return mAssetType
        else if(MFSPHelper.hasKey(ASSET_TYPE)){
            val readObject = MFSPHelper.readObject(ASSET_TYPE, AssetTypeModel::class.java)
            mAssetType = readObject
            return mAssetType
        }
        return null
    }

    fun setAddressInfo(addressModel:ArrayList<AddressModel>?){
        addressModel?.run {
            mAddressInfo = addressModel
            MFSPHelper.setObjectList(ADDRESS_INFO,addressModel)
        }
    }

    fun getAddressInfo():List<AddressModel>?{
        if(mAddressInfo != null)
            return mAddressInfo
        else if(MFSPHelper.hasKey(ADDRESS_INFO)){
            val readObject = MFSPHelper.getObjectList(ADDRESS_INFO, AddressModel::class.java)
            mAddressInfo = readObject
            return mAddressInfo
        }
        return null
    }

    fun setDepartmentInfo(departmentModel:DepartmentModel?){
        departmentModel?.run {
            mDepartmentInfo = departmentModel
            MFSPHelper.saveObject(DEPARTMENT_INFO,departmentModel)
        }
    }

    fun getDepartmentInfo():DepartmentModel?{
        if(mDepartmentInfo != null)
            return mDepartmentInfo
        else if(MFSPHelper.hasKey(DEPARTMENT_INFO)){
            val readObject = MFSPHelper.readObject(DEPARTMENT_INFO, DepartmentModel::class.java)
            mDepartmentInfo = readObject
            return mDepartmentInfo
        }
        return null
    }

    fun setDepartmentUserInfo(departmentModel:DepartmentModel?){
        departmentModel?.run {
            mDepartmentUserInfo = departmentModel
            MFSPHelper.saveObject(DEPARTMENT_USER_INFO,departmentModel)
        }
    }

    fun getDepartmentUserInfo():DepartmentModel?{
        if(mDepartmentUserInfo != null)
            return mDepartmentUserInfo
        else if(MFSPHelper.hasKey(DEPARTMENT_USER_INFO)){
            val readObject = MFSPHelper.readObject(DEPARTMENT_USER_INFO, DepartmentModel::class.java)
            mDepartmentUserInfo = readObject
            return mDepartmentUserInfo
        }
        return null
    }

    fun isLogin():Boolean{
        return !getToken().isNullOrEmpty()
    }


    fun loginOut(){
        MFSPHelper.remove(USER_INFO)
        MFSPHelper.remove(TOKEN)
        MFSPHelper.remove(ASSET_TYPE)
        MFSPHelper.remove(DEPARTMENT_INFO)
        MFSPHelper.remove(DEPARTMENT_USER_INFO)
        MFSPHelper.remove(ADDRESS_INFO)
        mAssetType = null
        mDepartmentInfo= null
        mDepartmentUserInfo = null
        mAddressInfo = null
        ActivityManager.instance.getTopActivity()?.gotoActivity<LoginActivity> {  }
    }
}